package org.sbbs.app.security.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.sbbs.app.security.dao.MenuDao;
import org.sbbs.app.security.model.Menu;
import org.sbbs.app.security.model.UrlSecurityResource;
import org.sbbs.app.security.service.MenuManager;
import org.sbbs.base.pagequery.hibernate.CustomCriteria;
import org.sbbs.base.pager.IPager;
import org.sbbs.base.searcher.SearchFilter;
import org.sbbs.base.service.impl.BaseTreeManagerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("menuManager")
public class MenuManagerImpl extends BaseTreeManagerImpl<Menu, Long>
		implements
			MenuManager {
	private MenuDao menuDao;

	@Autowired
	public MenuManagerImpl(MenuDao menuDao) {
		super(menuDao);
		this.menuDao = menuDao;
	}

	@Override
	public void initMenuFromUrlResource(List urls) {
		Menu rm = this.getRootMenu();
		List ml = new ArrayList();
		for (Iterator iterator = urls.iterator(); iterator.hasNext();) {
			UrlSecurityResource url = (UrlSecurityResource) iterator.next();
			Menu m = new Menu();
			m.setParentNode(rm);
			m.setTitle(url.getName());
			m.setUrlResource(url);
			ml.add(m);
		}
		this.menuDao.initMenu(ml);
	}

	@Override
	public Menu getRootMenu() {
		List l = this.getAllRoots();
		return (Menu) l.get(0);
	}


}
